<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>AngelScript: Pre-compiled byte code</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>


</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   
   </div>
   
  </td>
  
  
  
   
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('doc_adv_precompile.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">Pre-compiled byte code </div>  </div>
</div>
<div class="contents">
<div class="textblock"><p>Sometimes it may be useful to use pre-compiled bytecode instead of re-building the scripts every time a script is loaded. This can for example reduce load times, or if scripts are transferred over the net, it can reduce the transfer time for large scripts.</p>
<p>To save already compiled code you should call the <a class="el" href="classas_i_script_module.html#a493e91e8ccf282966881276e2022dab1">SaveByteCode</a> method on the <a class="el" href="classas_i_script_module.html">asIScriptModule</a>, passing in a pointer to the object that implements the <a class="el" href="classas_i_binary_stream.html">asIBinaryStream</a> interface. The script module will then push all the bytecode and related data onto this object with the <a class="el" href="classas_i_binary_stream.html#a1fce5bbea004d6b2705f9eefae1a3770">Write</a> method.</p>
<p>When loading the pre-compiled bytecode you should first get an instance of the <a class="el" href="classas_i_script_module.html">asIScriptModule</a>, just as if compiling a new script. Instead of calling AddScriptSection followed by Build you will call the <a class="el" href="classas_i_script_module.html#a2cf2e0fa7de8a14034eb31aa3edecbd0">LoadByteCode</a> method, passing it a pointer to the binary stream object. The script module will then pull all the bytecode and related data from that object with the <a class="el" href="classas_i_binary_stream.html#ab69b772db0a4157a842d90070e6fa1dc">Read</a> method to reconstruct the script module.</p>
<p>Here's a very simple example of how to implement the <a class="el" href="classas_i_binary_stream.html">asIBinaryStream</a> interface:</p>
<div class="fragment"><pre class="fragment"><span class="keyword">class </span>CBytecodeStream : <span class="keyword">public</span> <a class="code" href="classas_i_binary_stream.html" title="A binary stream interface.">asIBinaryStream</a>
{
<span class="keyword">public</span>:
  CBytecodeStream(FILE *fp) : f(fp) {}

  <span class="keywordtype">void</span> <a class="code" href="classas_i_binary_stream.html#a1fce5bbea004d6b2705f9eefae1a3770" title="Write size bytes to the stream from the memory pointed to by ptr.">Write</a>(<span class="keyword">const</span> <span class="keywordtype">void</span> *ptr, <a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815" title="32 bit unsigned integer">asUINT</a> size) 
  {
    <span class="keywordflow">if</span>( size == 0 ) <span class="keywordflow">return</span>; 
    fwrite(ptr, size, 1, f); 
  }
  <span class="keywordtype">void</span> <a class="code" href="classas_i_binary_stream.html#ab69b772db0a4157a842d90070e6fa1dc" title="Read size bytes from the stream into the memory pointed to by ptr.">Read</a>(<span class="keywordtype">void</span> *ptr, <a class="code" href="angelscript_8h.html#ac8186f029686800b7ce36bde4a55c815" title="32 bit unsigned integer">asUINT</a> size) 
  { 
    <span class="keywordflow">if</span>( size == 0 ) <span class="keywordflow">return</span>; 
    fread(ptr, size, 1, f); 
  }

<span class="keyword">protected</span>:
  FILE *f;
};
</pre></div><dl class="see"><dt><b>See also:</b></dt><dd><a class="el" href="doc_samples_asbuild.html">Generic compiler</a></dd></dl>
<h2><a class="anchor" id="doc_adv_precompile_1"></a>
Things to remember</h2>
<ul>
<li>All the objects, methods, properties, etc, used when compiling the bytecode must also be registered when loading the precompiled bytecode, otherwise the load will fail when not finding the correct entity.</li>
</ul>
<ul>
<li>The script engine doesn't perform validation of the pre-compiled bytecode, so the application is responsible for authenticating the source. If this authentication isn't performed by the application you'll have potential security risks in your application as the bytecode may have been manually manipulated to perform otherwise illegal tasks.</li>
</ul>
<ul>
<li>If the application that compiles the script code is separate from the application that will execute them, then you may register the functions and methods with null pointers so you don't need to actually implement all of them. Object properties must be registered with different offsets so that they can be differentiated by the bytecode loader. You should also set the engine property <a class="el" href="angelscript_8h.html#a53c2e8a74ade77c928316396394ebe0facac241d97facce4eaf9e5b0ca40dfcf1">asEP_INIT_GLOBAL_VARS_AFTER_BUILD</a> to false with <a class="el" href="classas_i_script_engine.html#a1bce4e5f573a2ca0ff55163e28f761dd">asIScriptEngine::SetEngineProperty</a>, so the script engine doesn't attempt to initialize the global variables after the script has been built.</li>
</ul>
<ul>
<li>The saved bytecode is almost fully platform independent, with the exception of the difference in pointer size. A script compiled on a 32bit platform will generally not work on a 64bit platform and vice versa. </li>
</ul>
</div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>

    <li class="footer">Generated on Sun Jan 29 2012 15:41:03 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.5.1 </li>
   </ul>
 </div>


</body>
</html>
